﻿using ServiceCore.Tool.Xml;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;

namespace ServiceCore.Tool.Wx
{
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 定义微信消息回复model
    /// </summary>
    public class wx_msg_base_model
    { 
        /// <summary>
        /// 开发者微信号
        /// </summary>
        public string ToUserName { get; set; }
        /// <summary>
        /// 发送方帐号（一个OpenID）
        /// </summary>
        public string FromUserName { get; set; }
        /// <summary>
        /// 消息创建时间 （整型）
        /// </summary>
        public long CreateTime { get; set; } = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds);
        /// <summary>
        /// 消息类型，文本为text
        /// </summary>
        public string MsgType { get; set; }
    }
    #region 接收普通消息

    public class analysis_wx_msg
    {
        /// <summary>
        /// 针对普通推送的返回
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="do_next"></param>
        public static void serialize(string xml, Action<string, object> do_next)
        {
            try
            {
                var base_data = XMLConvert.Get_DTO<wx_msg_base_model>(xml);
                Assembly assembly = Assembly.Load(new AssemblyName("ServiceCore"));
                Type type = assembly.GetType($"ServiceCore.Tool.Wx.{base_data.MsgType}_msg");
                if (type == null)
                {
                    do_next(base_data.MsgType, null);
                }
                else
                {
                    object json_obj = XMLConvert.Get_DTO_to_obj(type, xml);
                    do_next(base_data.MsgType, json_obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 针对事件推送的返回
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="do_next"></param>
        public static void evnet_serialize(string xml, Action<string, object> do_next)
        {
            try
            {
                var base_data = XMLConvert.Get_DTO<event_msg>(xml);
                Assembly assembly = Assembly.Load(new AssemblyName("ServiceCore"));
                Type type = assembly.GetType($"ServiceCore.Tool.Wx.event_{base_data.Event.ToLower()}_msg");
                if (type == null)
                {
                    do_next(base_data.Event.ToLower(), null);
                }
                else
                {
                    object json_obj = XMLConvert.Get_DTO_to_obj(type, xml);
                    do_next(base_data.Event.ToLower(), json_obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 文本消息
    /// </summary>
    public class text_msg : wx_msg_base_model
    {
        /// <summary>
        /// 	文本消息内容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 消息id，64位整型
        /// </summary>
        public long MsgId { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 图片消息
    /// </summary>
    public class image_msg : wx_msg_base_model
    {
        /// <summary>
        /// 图片链接（由系统生成）
        /// </summary>
        public string PicUrl { get; set; }
        /// <summary>
        /// 图片消息媒体id，可以调用获取临时素材接口拉取数据。
        /// </summary>
        public string MediaId { get; set; }
        /// <summary>
        /// 消息id，64位整型
        /// </summary>
        public long MsgId { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 语音消息
    /// </summary>
    public class voice_msg : wx_msg_base_model
    {
        /// <summary>
        /// 语音消息媒体id，可以调用获取临时素材接口拉取数据。
        /// </summary>
        public string MediaId { get; set; }
        /// <summary>
        /// 消息id，64位整型
        /// </summary>
        public long MsgId { get; set; }
        /// <summary>
        /// 语音格式，如amr，speex等
        /// </summary>
        public string Format { get; set; }
        /// <summary>
        /// 语音识别结果，UTF8编码
        /// </summary>
        public string Recognition { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 视频消息
    /// </summary>
    public class video_msg : wx_msg_base_model
    {
        /// <summary>
        /// 语音消息媒体id，可以调用获取临时素材接口拉取数据。
        /// </summary>
        public string MediaId { get; set; }
        /// <summary>
        /// 消息id，64位整型
        /// </summary>
        public long MsgId { get; set; }
        /// <summary>
        /// 视频消息缩略图的媒体id，可以调用多媒体文件下载接口拉取数据。
        /// </summary>
        public string ThumbMediaId { get; set; } 
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 小视频消息
    /// </summary>
    public class shortvideo_msg : wx_msg_base_model
    {
        /// <summary>
        /// 语音消息媒体id，可以调用获取临时素材接口拉取数据。
        /// </summary>
        public string MediaId { get; set; }
        /// <summary>
        /// 消息id，64位整型
        /// </summary>
        public long MsgId { get; set; }
        /// <summary>
        /// 视频消息缩略图的媒体id，可以调用多媒体文件下载接口拉取数据。
        /// </summary>
        public string ThumbMediaId { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 地理位置消息
    /// </summary>
    public class location_msg : wx_msg_base_model
    {
        /// <summary>
        /// 地理位置维度
        /// </summary>
        public double? Location_X { get; set; }
        /// <summary>
        /// 地理位置经度
        /// </summary>
        public double? Location_Y { get; set; }
        /// <summary>
        /// 地图缩放大小
        /// </summary>
        public int? Scale { get; set; }
        /// <summary>
        /// 地理位置信息
        /// </summary>
        public string Label { get; set; }
        /// <summary>
        /// 消息id，64位整型
        /// </summary>
        public long MsgId { get; set; } 
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 链接消息
    /// </summary>
    public class link_msg : wx_msg_base_model
    {
        /// <summary>
        /// 消息标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 消息描述
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        ///  消息链接
        /// </summary>
        public string Url { get; set; }
        /// <summary>
        /// 消息id，64位整型
        /// </summary>
        public long MsgId { get; set; }
    }

    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 事件推送
    /// </summary>
    public class event_msg : wx_msg_base_model
    {
        /// <summary>
        /// 事件
        /// </summary>
        public string Event { get; set; }   
    }
    #endregion
    #region 接收事件消息 需要继承event_msg
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 用户关注/取消关注事件 
    /// </summary>
    public class event_unsubscribe_msg : event_msg
    {
       
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 用户关注/取消关注事件 
    /// </summary>
    public class event_subscribe_msg: event_msg
    {
        #region 扫描带参数二维码 含有此项
        /// <summary>
        /// 事件KEY值，qrscene_为前缀，后面为二维码的参数值
        /// </summary>
        public string EventKey { get; set; }
        /// <summary>
        /// 二维码的ticket，可用来换取二维码图片
        /// </summary>
        public string Ticket { get; set; }
        #endregion 
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 用户关注/取消关注事件 
    /// </summary>
    public class event_scan_msg : event_subscribe_msg
    {

    }

   [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 上报地理位置事件
    /// 用户同意上报地理位置后，每次进入公众号会话时，都会在进入时上报地理位置，或在进入会话后每5秒上报一次地理位置，公众号可以在公众平台网站中修改以上设置。上报地理位置时，微信会将上报地理位置事件推送到开发者填写的URL。
    /// </summary>
    public class event_location_msg : event_msg
    {
        /// <summary>
        /// 	地理位置纬度
        /// </summary>
        public double Latitude { get; set; }
        /// <summary>
        /// 	地理位置经度
        /// </summary>
        public double Longitude { get; set; }
        /// <summary>
        /// 	地理位置精度
        /// </summary>
        public double Precision { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 自定义菜单事件
    /// </summary>
    public class event_click_msg : event_msg
    {
        /// <summary>
        /// 事件KEY值，与自定义菜单接口中KEY值对应
        /// </summary>
        public string EventKey { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 点击菜单跳转链接时的事件推送
    /// </summary>
    public class event_view_msg : event_msg
    {
        /// <summary>
        /// 事件KEY值，设置的跳转URL
        /// </summary>
        public string EventKey { get; set; }
    }
    #endregion

    #region 返回消息体
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 回复文本消息
    /// </summary>
    public class reply_text_msg: wx_msg_base_model
    {
  
        /// <summary>
        /// 回复的消息内容（换行：在content中能够换行，微信客户端就支持换行显示）
        /// </summary>
        public string Content { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 回复图片消息
    /// </summary>
    public class reply_image_msg : wx_msg_base_model
    {
    
        /// <summary>
        /// 
        /// </summary>
        public reply_image_item Image { get; set; }
    }
    public class reply_image_item
    {
        /// <summary>
        /// 通过素材管理中的接口上传多媒体文件，得到的id。
        /// </summary>
        public string MediaId { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 回复语音消息
    /// </summary>
    public class reply_voice_msg : wx_msg_base_model
    {
    
        /// <summary>
        /// 
        /// </summary>
        public reply_voice_item Voice { get; set; }
    }
    public class reply_voice_item
    {
        /// <summary>
        /// 通过素材管理中的接口上传多媒体文件，得到的id。
        /// </summary>
        public string MediaId { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 回复视频消息 
    /// </summary>
    public class reply_video_msg : wx_msg_base_model
    {
 
        /// <summary>
        /// 
        /// </summary>
        public reply_video_item Video { get; set; }
    }

    public class reply_video_item
    {
        /// <summary>
        /// 通过素材管理中的接口上传多媒体文件，得到的id。
        /// </summary>
        public string MediaId { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }
    }
    [XmlRoot("xml", Namespace = "")]
    /// <summary>
    /// 回复多图文 
    /// </summary>
    public class reply_news_msg : wx_msg_base_model
    {
    
        /// <summary>
        /// 图文消息个数；当用户发送文本、图片、语音、视频、图文、地理位置这六种消息时，开发者只能回复1条图文消息；其余场景最多可回复8条图文消息
        /// </summary>
        public int ArticleCount { get; set; }
        /// <summary>
        /// 图文消息信息，注意，如果图文数超过限制，则将只发限制内的条数
        /// </summary>
        public Articles Articles { get; set; }
    }

    public class Articles
    {
        [XmlElement(ElementName = "item")]
        /// <summary>
        /// 内部项
        /// </summary>
        public List<Articles_item> item { get; set; }
    }

    public class Articles_item {
        public string Title { get; set; } 

        public string Description { get; set; }
        /// <summary>
        /// 图片链接，支持JPG、PNG格式，较好的效果为大图360*200，小图200*200
        /// </summary>
        public string PicUrl { get; set; } 
        /// <summary>
        ///  点击图文消息跳转链接
        /// </summary>
        public string Url { get; set; }
    }
    #endregion
}
